﻿using BamaTeaWebAPI.Models;
using BamaTeaWebAPI.Services;
using FX_API;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebAPIHelper;

namespace BamaTeaWebAPI.Controllers
{
    public class FeiyuController : ApiController
    {
        /// <summary>
        /// 以json的方式接收飞鱼上传接口的参数
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage UploadClue(FeiyuRequestModel requestModel)
        {
            try
            {
                FenxiangAPI.FetchToken();
                FeiyuService service = new FeiyuService();
                bool isSuccess = service.UploadToFX(requestModel);

                if (!isSuccess) return ResponseMessageHelper.Fail;
                return ResponseMessageHelper.SUCCESS;
            }
            catch (Exception ex)
            {
                return ResponseMessageHelper.CreateErrorMessage(ex);
            }
        }

        /// <summary>
        /// 以FormData的方式接收飞鱼上传接口的参数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage UploadClueFormData()
        {
            try
            {
                var requestModel = GetFeiyuModelFromFormData();

                FeiyuService service = new FeiyuService();
                bool isSuccess = service.UploadToFX(requestModel);

                if (!isSuccess) return ResponseMessageHelper.Fail;

                return ResponseMessageHelper.SUCCESS;
            }
            catch (Exception ex)
            {
                return ResponseMessageHelper.CreateErrorMessage(ex);
            }
        }

        private FeiyuRequestModel GetFeiyuModelFromFormData()
        {
            var site_name = HttpContext.Current.Request.Params["site_name"];
            var adv_id = HttpContext.Current.Request.Params["adv_id"];
            var app_name = HttpContext.Current.Request.Params["app_name"];
            var name = HttpContext.Current.Request.Params["name"];
            var location = HttpContext.Current.Request.Params["location"];
            var city_name = HttpContext.Current.Request.Params["city_name"];
            var province_name = HttpContext.Current.Request.Params["province_name"];
            var telphone = HttpContext.Current.Request.Params["telphone"];

            return new FeiyuRequestModel()
            {
                site_name = site_name,
                adv_id = adv_id,
                app_name = app_name,
                name = name,
                location = location,
                city_name = city_name,
                province_name = province_name,
                telphone = telphone
            };
        }
    }
}
